//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    top: [{
      'title': '已购课程',
      'active': true,
      'type': 0
    },
    {
      'title': '浏览历史',
      'active': false,
      'type': 1
    }
    ],
    // 
    uid:"",
    imgUrl: baseJs.imgUrl,
    pageIndex:1,
    state:"",
    courseList:[],
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      uid: wx.getStorageSync('uid'),
    })
    this.GetMyCourseList();

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.data.pageIndex++;
    this.GetMyCourseList();
  },

  // 
  topTap: function (e) {
    var top = this.data.top;
    var type = e.currentTarget.dataset.type;
    for (var i = 0, len = top.length; i < len; ++i) {
      if (top[i].type == type) {
        top[i].active = true;
      } else {
        top[i].active = false
      }
    }
    this.setData({
      top: top,
      type: type
    })
  },
  //
  GetMyCourseList: function () {
    var that = this;
    var uid = this.data.uid;
    var pageIndex = this.data.pageIndex;
    var courseList = this.data.courseList;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetMyCourseList&PageSize=10&PageIndex=" + pageIndex + "&UID=" + uid),
      success: function (res) {
        var json = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        console.log(json);
        for (var i = 0; i < json.length; i++) {
          courseList.push(json[i])
        }
        that.setData({
          courseList: courseList,
        })
        if (json.length == 10) {
          that.setData({
            state: 'loadMore',
          })
        } else if (json.length == 0 && pageIndex == 1) {
          that.setData({
            state: 'noData',
          })
        } else if (json.length < 10) {
          that.setData({
            state: 'noMore',
          })
        } else if (json.length == 0 && pageIndex > 1) {
          that.setData({
            state: 'noMore',
          })
        }

      }
    })
  },
  // 
})